package com.example.zyfapplication.view.act;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.RectF;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Build;
import android.os.Bundle;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.baeshelplibrary.utils.LogUtil;
import com.example.zyfapplication.R;
import com.example.zyfapplication.base.BaseActivity;
import com.example.zyfapplication.utils.FaceView;
import com.example.zyfapplication.utils.FindAndDrowFaceUtils;
import com.example.zyfapplication.utils.OnMultiClickListener;
import com.example.zyfapplication.utils.TimeUtil;
import com.example.zyfapplication.utils.ToastUtil;

import java.io.IOException;
import java.util.List;

public class MainActivity extends BaseActivity {

    private Button btBottomOne;
    private TextureView txvShow;
    private ImageView ivShow;
    private FaceView faceKuang;
    private Camera mCamera;
    private Bitmap capBitmap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public int initLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {

        btBottomOne = findViewById(R.id.bt_bottom_select);
        txvShow = findViewById(R.id.txv_show);
        ivShow = findViewById(R.id.iv_image);
        faceKuang = findViewById(R.id.fv_kuang);


    }

    @Override
    protected void initPrePar() {
        getPermissions();
        // 首先获取摄像头 如果没有就不去操作来
        int numberOfCameras = Camera.getNumberOfCameras();
        if (numberOfCameras < 1) {
            ToastUtil.getInstance().show("没有相机");
            finish();
            return;
        }


    }

    @Override
    protected void initDate() {

        txvShow.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {

            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {


                mCamera = Camera.open(0);
                if (mCamera != null) {
                    // 设置相机预览宽高，此处设置为TextureView宽高
                    Camera.Parameters params = mCamera.getParameters();
                    params.setPreviewSize(width, height);
                    // 设置自动对焦模式
//                    List<String> focusModes = params.getSupportedFocusModes();
//                    if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
//                        params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
//                        mCamera.setParameters(params);
//                    }
                    try {
                        mCamera.setDisplayOrientation(90);// 设置预览角度，并不改变获取到的原始数据方向
                        // 绑定相机和预览的View
                        mCamera.setPreviewTexture(surface);

                        capBitmap = Bitmap.createBitmap(height, width, Bitmap.Config.ARGB_8888);

                        // 开始预览
                        mCamera.startPreview();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                mCamera.stopPreview();
                mCamera.release();
                return false;

            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface) {
                //这个地方 每次都会回调，所以 这里获取每一帧的图片，为后续处理
                Bitmap bitmap = txvShow.getBitmap(capBitmap);
                //由于这个方法不可以做耗时操作，所以就单纯的拿数据  至于处理，放线程，还是 服务 自己看着办 这里先放线程池
//                if (isFinish){
//                    isFinish = false;
                    FindAndDrowFaceUtils.findFaceFromBitmap(bitmap);
//                }


            }
        });

        btBottomOne.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
//                startActivity(new Intent(MainActivity.this, BottomMainActivity.class));
            }
        });

        FindAndDrowFaceUtils.SetHaveFindFaceListener(new FindAndDrowFaceUtils.haveFindFace() {
            @Override
            public void haveFace(final int number) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ToastUtil.getInstance().show("人脸出现了" + number);
                    }
                });

            }

            @Override
            public void ShowFace(final List<RectF> faces) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        faceKuang.setFaces(faces);
                    }
                });
            }

            @Override
            public void isFinish() {

            }
        });

    }

    @Override
    protected void initRequltResult() {

    }

    private void getPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this,
                    Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this,
                    Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this,
                    Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { //表示未授权时
                //进行授权
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA, Manifest.permission.READ_PHONE_STATE, Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            } else {

            }
        }
    }

    /**
     * 权限申请返回结果
     *
     * @param requestCode  请求码
     * @param permissions  权限数组
     * @param grantResults 申请结果数组，里面都是int类型的数
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1:
                // grantResults.length > 0&& grantResults[0] == PackageManager.PERMISSION_GRANTED
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED && grantResults[2] == PackageManager.PERMISSION_GRANTED) { //同意权限申请
                    // Toast.makeText(this,"同意权限申请",Toast.LENGTH_SHORT).show();
                    ToastUtil.getInstance().show("同意权限申请");
                } else { //拒绝权限申请
                    //Toast.makeText(this,"权限被拒绝了",Toast.LENGTH_SHORT).show();
                    ToastUtil.getInstance().show("权限被拒绝了");
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
